home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / mesa / mesa-tk / samples.tk / olympic.c < prev    next >
C/C++ Source or Header  |  2000-02-23  |  9KB  |  417 lines

  1. /*
  2.  * Copyright (c) 1991, 1992, 1993 Silicon Graphics, Inc.
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee, provided
  6.  * that (i) the above copyright notices and this permission notice appear in
  7.  * all copies of the software and related documentation, and (ii) the name of
  8.  * Silicon Graphics may not be used in any advertising or
  9.  * publicity relating to the software without the specific, prior written
  10.  * permission of Silicon Graphics.
  11.  *
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
  13.  * ANY KIND,
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16.  *
  17.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. /*
  26.  * Nov 20, 1995 use stdlib's rand()/srand() instead of random()/srand48(), etc.
  27.  */
  28.  
  29. /*
  30.  * Modified by Li Wei(liwei@aiar.xjtu.edu.cn) to be able to run in Windows
  31.  * 6/13
  32.  *
  33.  * Modified by Brian Paul to compile with Windows OR Unix.  7/23/97
  34.  */
  35.  
  36. #define _HPUX_SOURCE
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <math.h>
  42. #include <sys/types.h>
  43. #ifndef __WIN32__
  44. #include <sys/time.h>
  45. #endif
  46. #include "gltk.h"
  47.  
  48. #ifndef RAND_MAX
  49. #define RAND_MAX 32767
  50. #endif
  51.  
  52. #define XSIZE    100
  53. #define YSIZE    75
  54.  
  55. #define RINGS 5
  56. #define BLUERING 0
  57. #define BLACKRING 1
  58. #define REDRING 2
  59. #define YELLOWRING 3
  60. #define GREENRING 4
  61.  
  62. #define BACKGROUND 8
  63.  
  64. enum {
  65.   BLACK = 0,
  66.   RED,
  67.   GREEN,
  68.   YELLOW,
  69.   BLUE,
  70.   MAGENTA,
  71.   CYAN,
  72.   WHITE
  73. };
  74.  
  75. GLenum rgb, doubleBuffer, directRender;
  76.  
  77. unsigned char rgb_colors[RINGS][3];
  78. int mapped_colors[RINGS];
  79. float dests[RINGS][3];
  80. float offsets[RINGS][3];
  81. float angs[RINGS];
  82. float rotAxis[RINGS][3];
  83. int iters[RINGS];
  84. GLuint theTorus;
  85.  
  86. void FillTorus(float rc, int numc, float rt, int numt)
  87. {
  88.   int i, j, k;
  89.   double s, t;
  90.   double x, y, z;
  91.   double pi, twopi;
  92.  
  93.   pi = 3.14159265358979323846;
  94.   twopi = 2 * pi;
  95.  
  96.   for (i = 0; i < numc; i++) {
  97.     glBegin(GL_QUAD_STRIP);
  98.     for (j = 0; j <= numt; j++) {
  99.       for (k = 1; k >= 0; k--) {
  100.     s = (i + k) % numc + 0.5;
  101.     t = j % numt;
  102.  
  103.     x = cos(t * twopi / numt) * cos(s * twopi / numc);
  104.     y = sin(t * twopi / numt) * cos(s * twopi / numc);
  105.     z = sin(s * twopi / numc);
  106.     glNormal3f(x, y, z);
  107.  
  108.     x = (rt + rc * cos(s * twopi / numc)) * cos(t * twopi / numt);
  109.     y = (rt + rc * cos(s * twopi / numc)) * sin(t * twopi / numt);
  110.     z = rc * sin(s * twopi / numc);
  111.     glVertex3f(x, y, z);
  112.       }
  113.     }
  114.     glEnd();
  115.   }
  116. }
  117.  
  118. float Clamp(int iters_left, float t)
  119. {
  120.  
  121.   if (iters_left < 3) {
  122.     return 0.0;
  123.   }
  124.   return (iters_left - 2) * t / iters_left;
  125. }
  126.  
  127. void DrawScene(void)
  128. {
  129.   int i, j;
  130.   GLboolean goIdle;
  131.  
  132.   goIdle = GL_TRUE;
  133.   for (i = 0; i < RINGS; i++) {
  134.     if (iters[i]) {
  135.       for (j = 0; j < 3; j++) {
  136.     offsets[i][j] = Clamp(iters[i], offsets[i][j]);
  137.       }
  138.       angs[i] = Clamp(iters[i], angs[i]);
  139.       iters[i]--;
  140.       goIdle = GL_FALSE;
  141.     }
  142.   }
  143.   if (goIdle) {
  144.     tkIdleFunc(NULL);
  145.   }
  146.  
  147.   glPushMatrix();
  148.  
  149.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  150.   gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
  151.  
  152.   for (i = 0; i < RINGS; i++) {
  153.     if (rgb) {
  154.       glColor3ubv(rgb_colors[i]);
  155.     }
  156.     else {
  157.       glIndexi(mapped_colors[i]);
  158.     }
  159.     glPushMatrix();
  160.     glTranslatef(dests[i][0] + offsets[i][0], dests[i][1] + offsets[i][1],
  161.          dests[i][2] + offsets[i][2]);
  162.     glRotatef(angs[i], rotAxis[i][0], rotAxis[i][1], rotAxis[i][2]);
  163.     glCallList(theTorus);
  164.     glPopMatrix();
  165.   }
  166.  
  167.   glPopMatrix();
  168.  
  169.   glFlush();
  170.   if (doubleBuffer) {
  171.     tkSwapBuffers();
  172.   }
  173. }
  174.  
  175. float MyRand(void)
  176. {
  177.   return 10.0 * ((float)rand() / (float)RAND_MAX - 0.5);
  178. }
  179.  
  180. void ReInit(void)
  181. {
  182.   int i;
  183.   float deviation;
  184.  
  185.   deviation = MyRand() / 2;
  186.   deviation = deviation * deviation;
  187.   for (i = 0; i < RINGS; i++) {
  188.     offsets[i][0] = MyRand();
  189.     offsets[i][1] = MyRand();
  190.     offsets[i][2] = MyRand();
  191.     angs[i] = 260.0 * MyRand();
  192.     rotAxis[i][0] = MyRand();
  193.     rotAxis[i][1] = MyRand();
  194.     rotAxis[i][2] = MyRand();
  195.     iters[i] = (deviation * MyRand() + 60.0);
  196.   }
  197.   tkIdleFunc(DrawScene);
  198. }
  199.  
  200. void Init(void)
  201. {
  202.   float base, height;
  203.   float aspect, x, y;
  204.   int i;
  205.  
  206. #ifdef __WIN32__
  207.   long t;
  208.  
  209. #else
  210.   struct timeval t;
  211.  
  212. #endif
  213.  
  214.   float top_y = 1.0;
  215.   float bottom_y = 0.0;
  216.   float top_z = 0.15;
  217.   float bottom_z = 0.69;
  218.   float spacing = 2.5;
  219.   static float lmodel_ambient[] =
  220.   {0.0, 0.0, 0.0, 0.0};
  221.   static float lmodel_twoside[] =
  222.   {GL_FALSE};
  223.   static float lmodel_local[] =
  224.   {GL_FALSE};
  225.   static float light0_ambient[] =
  226.   {0.1, 0.1, 0.1, 1.0};
  227.   static float light0_diffuse[] =
  228.   {1.0, 1.0, 1.0, 0.0};
  229.   static float light0_position[] =
  230.   {0.8660254, 0.5, 1, 0};
  231.   static float light0_specular[] =
  232.   {1.0, 1.0, 1.0, 0.0};
  233.   static float bevel_mat_ambient[] =
  234.   {0.0, 0.0, 0.0, 1.0};
  235.   static float bevel_mat_shininess[] =
  236.   {40.0};
  237.   static float bevel_mat_specular[] =
  238.   {1.0, 1.0, 1.0, 0.0};
  239.   static float bevel_mat_diffuse[] =
  240.   {1.0, 0.0, 0.0, 0.0};
  241.  
  242. #ifdef __WIN32__
  243.   t = clock();
  244.   srand(t);
  245. #else
  246.   gettimeofday(&t, NULL);
  247.   srand(t.tv_usec);
  248. #endif
  249.  
  250.   ReInit();
  251.   for (i = 0; i < RINGS; i++) {
  252.     rgb_colors[i][0] = rgb_colors[i][1] = rgb_colors[i][2] = 0;
  253.   }
  254.   rgb_colors[BLUERING][2] = 255;
  255.   rgb_colors[REDRING][0] = 255;
  256.   rgb_colors[GREENRING][1] = 255;
  257.   rgb_colors[YELLOWRING][0] = 255;
  258.   rgb_colors[YELLOWRING][1] = 255;
  259.   mapped_colors[BLUERING] = BLUE;
  260.   mapped_colors[REDRING] = RED;
  261.   mapped_colors[GREENRING] = GREEN;
  262.   mapped_colors[YELLOWRING] = YELLOW;
  263.   mapped_colors[BLACKRING] = BLACK;
  264.  
  265.   dests[BLUERING][0] = -spacing;
  266.   dests[BLUERING][1] = top_y;
  267.   dests[BLUERING][2] = top_z;
  268.  
  269.   dests[BLACKRING][0] = 0.0;
  270.   dests[BLACKRING][1] = top_y;
  271.   dests[BLACKRING][2] = top_z;
  272.  
  273.   dests[REDRING][0] = spacing;
  274.   dests[REDRING][1] = top_y;
  275.   dests[REDRING][2] = top_z;
  276.  
  277.   dests[YELLOWRING][0] = -spacing / 2.0;
  278.   dests[YELLOWRING][1] = bottom_y;
  279.   dests[YELLOWRING][2] = bottom_z;
  280.  
  281.   dests[GREENRING][0] = spacing / 2.0;
  282.   dests[GREENRING][1] = bottom_y;
  283.   dests[GREENRING][2] = bottom_z;
  284.  
  285.   base = 2.0;
  286.   height = 2.0;
  287.   theTorus = glGenLists(1);
  288.   glNewList(theTorus, GL_COMPILE);
  289.   FillTorus(0.1, 8, 1.0, 25);
  290.   glEndList();
  291.  
  292.   x = (float)XSIZE;
  293.   y = (float)YSIZE;
  294.   aspect = x / y;
  295.   glEnable(GL_CULL_FACE);
  296.   glCullFace(GL_BACK);
  297.   glEnable(GL_DEPTH_TEST);
  298.   glClearDepth(1.0);
  299.  
  300.   if (rgb) {
  301.     glClearColor(0.5, 0.5, 0.5, 0.0);
  302.     glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
  303.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
  304.     glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
  305.     glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
  306.     glEnable(GL_LIGHT0);
  307.  
  308.     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, lmodel_local);
  309.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  310.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  311.     glEnable(GL_LIGHTING);
  312.  
  313.     glMaterialfv(GL_FRONT, GL_AMBIENT, bevel_mat_ambient);
  314.     glMaterialfv(GL_FRONT, GL_SHININESS, bevel_mat_shininess);
  315.     glMaterialfv(GL_FRONT, GL_SPECULAR, bevel_mat_specular);
  316.     glMaterialfv(GL_FRONT, GL_DIFFUSE, bevel_mat_diffuse);
  317.  
  318.     glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  319.     glEnable(GL_COLOR_MATERIAL);
  320.     glShadeModel(GL_SMOOTH);
  321.   }
  322.   else {
  323.     glClearIndex(BACKGROUND);
  324.     glShadeModel(GL_FLAT);
  325.   }
  326.  
  327.   glMatrixMode(GL_PROJECTION);
  328.   gluPerspective(45, 1.33, 0.1, 100.0);
  329.   glMatrixMode(GL_MODELVIEW);
  330. }
  331.  
  332. void Reshape(int width, int height)
  333. {
  334.  
  335.   glViewport(0, 0, width, height);
  336. }
  337.  
  338. GLenum Key(int key, GLenum mask)
  339. {
  340.  
  341.   switch (key) {
  342.     case TK_ESCAPE:
  343.       tkQuit();
  344.     case TK_SPACE:
  345.       ReInit();
  346.       break;
  347.     default:
  348.       return GL_FALSE;
  349.   }
  350.   return GL_TRUE;
  351. }
  352.  
  353. GLenum Args(int argc, char **argv)
  354. {
  355.   GLint i;
  356.  
  357.   rgb = GL_TRUE;
  358.   doubleBuffer = GL_FALSE;
  359.   directRender = GL_TRUE;
  360.  
  361.   for (i = 1; i < argc; i++) {
  362.     if (strcmp(argv[i], "-ci") == 0) {
  363.       rgb = GL_FALSE;
  364.     }
  365.     else if (strcmp(argv[i], "-rgb") == 0) {
  366.       rgb = GL_TRUE;
  367.     }
  368.     else if (strcmp(argv[i], "-sb") == 0) {
  369.       doubleBuffer = GL_FALSE;
  370.     }
  371.     else if (strcmp(argv[i], "-db") == 0) {
  372.       doubleBuffer = GL_TRUE;
  373.     }
  374.     else if (strcmp(argv[i], "-dr") == 0) {
  375.       directRender = GL_TRUE;
  376.     }
  377.     else if (strcmp(argv[i], "-ir") == 0) {
  378.       directRender = GL_FALSE;
  379.     }
  380.     else {
  381.       printf("%s (Bad option).\n", argv[i]);
  382.       return GL_FALSE;
  383.     }
  384.   }
  385.   return GL_TRUE;
  386. }
  387.  
  388. void main(int argc, char **argv)
  389. {
  390.   GLenum type;
  391.  
  392.   if (Args(argc, argv) == GL_FALSE) {
  393.     tkQuit();
  394.   }
  395.  
  396.   tkInitPosition(0, 0, 400, 300);
  397.  
  398.   type = TK_DEPTH;
  399.   type |= (rgb) ? TK_RGB : TK_INDEX;
  400.   type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  401.   type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  402.   tkInitDisplayMode(type);
  403.  
  404.   if (tkInitWindow("Olympic") == GL_FALSE) {
  405.     tkQuit();
  406.   }
  407.  
  408.   Init();
  409.  
  410.   tkExposeFunc(Reshape);
  411.   tkReshapeFunc(Reshape);
  412.   tkKeyDownFunc(Key);
  413.   tkIdleFunc(DrawScene);
  414.  
  415.   tkExec();
  416. }
  417.